package com.shujia.spark.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo13Sort {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("Demo13Sort")

    val sc = new SparkContext(conf)

    val kvRDD: RDD[(String, Int)] = sc.textFile("data/score.txt")
      .map(_.split(","))
      .filter(_.length == 3)
      .map {
        case Array(sid: String, _: String, sco: String) =>
          (sid, sco.toInt)
      }

    val sumScoRDD: RDD[(String, Int)] = kvRDD.reduceByKey(_ + _)

    /**
     * sortBy: 指定一个字段进行排序，默认是升序
     * ascending = false： 降序
     *
     */
    val sortRDD: RDD[(String, Int)] = sumScoRDD.sortBy(kv => kv._2, ascending = false)

    sortRDD.foreach(println)

  }

}
